define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Hongbaolist, GoDialog, GoPage, Auth, $window, $ionicModal) {
		
		$scope.isLogin = true;//是否登录，不登录不能进入红包详情
		
		$scope.$on('$ionicView.afterEnter', function() {
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
			var all = $window.innerHeight;
			$scope.myObj = {
				"height": all - 200 - 44 - lan + "px",
				"margin-top": 100 + "px",
				"margin-bottom": 100 + "px"
			}
			$scope.init_data();
		}, false);
		
		//初始化页面
		$scope.init_data = function(){
			$scope.session = Auth.get_user_id();
			if($scope.session == ""){
				$scope.isLogin = false;//没有登录
				$scope.session = Auth.get_session();
			}
			Hongbaolist.getlist($scope.session,function(data) {
				$scope.datas = data;
//				console.log(data)
			})
		}
		
		//红包详情。如果没有登录，就先去登录
		$scope.onshow_detail = function(id,is_end) {
			if(is_end) return;//已结束的，不能进入详情
			if($scope.isLogin){
				GoPage.go_states("hongbaodetail", {id: id});
			}else{
				GoDialog.alert("提示", "请先登录",function(){
					GoPage.go_states("t-login");
				});
			}
		};
		
		
		/****************上下拉刷新****************/
		$scope.isLoad = false; //可以加载
		$scope.doRefresh = function() {
			//防止重复加载
			Hongbaolist.getlist($scope.session,function(data) {
				$scope.datas = data;
				console.log(data)
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
			});
		};
		/****************上下拉刷新****************/
		
		/***********显示《红包雨规则》**********/
		$ionicModal.fromTemplateUrl("templates/modal/redrain_rule.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_rule = modal;
		});
		//打开
		$scope.openModal_rule = function() {
			$scope.modal_rule.show();
		}
		//隐藏
		$scope.closeModal_rule = function() {
			$scope.modal_rule.hide();
		}
		/***********显示《红包雨规则》**********/
		
		
	}

	controller.$inject = ['$scope', 'Hongbaolist', 'GoDialog', 'GoPage','Auth', '$window','$ionicModal'];

	return app.registerController("hongbaolistctrl", controller);
});